package com.itstherules.jmsunit.loaders;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.custommonkey.xmlunit.XMLUnit;
import org.xml.sax.SAXException;

public abstract class AbstractXmlLoader implements FileLoader {

	private final FileLoader delegate;

	public AbstractXmlLoader() {
		delegate = new ResourceFileLoader();
	}

	public final String load(String file) throws FileNotFoundException {
		String xml = delegate.load(getFullPath(file));
		try {
			XMLUnit.buildControlDocument(xml);
		} 
		catch (SAXException e) { throw new IllegalArgumentException("the xml file " + xml + " is not well formed"); } 
		catch (IOException e) { throw new RuntimeException(e); }
		return xml;
	}
	
	public abstract String getFullPath(String file);

}
